<?php

class PersonaController extends GxController {


	public function actionView($id) {
		$this->render('view', array(
			'model' => $this->loadModel($id, 'Persona'),
		));
	}

	public function actionCreate() {
		$model = new Persona;

		$this->performAjaxValidation($model, 'persona-form');

		if (isset($_POST['Persona'])) {
			$model->setAttributes($_POST['Persona']);

			if ($model->save()) {
				if (Yii::app()->getRequest()->getIsAjaxRequest())
					Yii::app()->end();
				else
					$this->redirect(array('view', 'id' => $model->PersonaId));
			}
		}

		$this->render('create', array( 'model' => $model));
	}

	public function actionUpdate($id) {
		$model = $this->loadModel($id, 'Persona');

		$this->performAjaxValidation($model, 'persona-form');

		if (isset($_POST['Persona'])) {
			$model->setAttributes($_POST['Persona']);

			if ($model->save()) {
				$this->redirect(array('view', 'id' => $model->PersonaId));
			}
		}

		$this->render('update', array(
				'model' => $model,
				));
	}

	public function actionDelete($id) {
		if (Yii::app()->getRequest()->getIsPostRequest()) {
			$this->loadModel($id, 'Persona')->delete();

			if (!Yii::app()->getRequest()->getIsAjaxRequest())
				$this->redirect(array('admin'));
		} else
			throw new CHttpException(400, Yii::t('app', 'Your request is invalid.'));
	}

	public function actionIndex() {
		$dataProvider = new CActiveDataProvider('Persona');
		$this->render('index', array(
			'dataProvider' => $dataProvider,
		));
	}

	public function actionAdmin() {
		$model = new Persona('search');
		$model->unsetAttributes();

		if (isset($_GET['Persona']))
			$model->setAttributes($_GET['Persona']);

		$this->render('admin', array(
			'model' => $model,
		));
	}
	
	public function actionSelectCiudad() {
		if (isset($_POST['Persona']['ProvinciaId'])) { 
			$id = $_POST['Persona']['ProvinciaId'];
// 			$lista = Ciudad::model()->findAll('provinciaid = :id', array(':id' => $id));
			$lista = Ciudad::model()->findAll(array('order'=>'Descripcion', 'condition'=>'provinciaid = :id', 'params'=>array(':id' => $id)));
			echo CHtml::tag('option', array('value' => ''), 'Seleccione una Ciudad ... ', true);
			foreach($lista as $ciudad) {
				$valor = $ciudad['CiudadId'];
				$descripcion = $ciudad['Descripcion'];
				echo CHtml::tag('option', array('value' => $valor), CHtml::encode($descripcion), true);
			}
		}
	}	

}